<div class="box">
  <div class="box-header">
    <h3 class="box-title">机构部门列表</h3>
    <div class="box-tools">
      <span onclick="list_add('');" class="btn btn-info btn-sm">添加</span>
    </div>
  </div>
  <div class="box-body no-padding">
    
    <table id="table_list" class="table table-bordered table-striped">
      <thead>
        <tr class="text-c">
          <th width="60">ID</th>
          <th>机构名称</th>
          <th>联系地址</th>
          <th>邮政编码</th>
          <th>负责人</th>
          <th>电话</th>
          
          <th width="228px">操作</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>
</div>

<script type="text/javascript">

function list_add(pid){
	var params = "?parentId="+pid;
	HtmlRender('${BASE}/admin/sys/SysDept/add'+params);
}
function list_del(id){
	var select_ids = "";
	if(id){
		select_ids=id;
	}else{
	    $("input[name='table_checkbox_id']:checked").each(function(){
	    	select_ids+=$(this).val()+",";   
	    });
	    if(""==select_ids){
	    	layer.msg('请先选择要删除的数据！', {icon: 3,offset: '45px'});
	    	return false;
	    }
	}
	layer.confirm('确认删除所选记录?', {icon: 3, title:'警告'}, function(index){	    
	    layer.close(index);
	    layer.load(1, {shade: [0.3,'#fff']});
	    $.post("${BASE}/admin/sys/SysDept/delete",{ids:select_ids},function(jsondata){
			layer.closeAll('loading');
			if( jsondata.code == "001" ){
				layer.msg(jsondata.msg, {icon: 6,offset: '45px'});
				load_table();
			}else{
				layer.msg(jsondata.msg, {icon: 5,offset: '45px'});
			alert("XX");
			}
	    },"json");
	});
}
function list_edit(id){
    HtmlRender("${BASE}/admin/sys/SysDept/edit",{id:id});
}
function list_show(id){
	$.post("${BASE}/admin/sys/SysDept/show",{id:id}, function(str){
		layer.open({
			type: 1,
			shadeClose:true,
			title :'信息',
			area: ['800px', '500px'],
			content: str
		});
	});
}

function showTableBody(nodes){
	for(node in nodes ){
		var menu = nodes[node];
		var tdStr = "<td class='text-c'>"+menu.id+"</td><td>"
					+"<a href='javascript:;' onclick='list_show(\""+menu.id+"\")'>"+menu.name+"</a></td><td>"
					+menu.address+"</td><td>"
					+menu.zipCode+"</td><td>"
					+menu.master+"</td><td>"
					+menu.phone+"</td><td>"
					+'<a class="btn btn-info btn-xs" href="javascript:;" title="编辑" onclick="list_edit(\''+menu.id+'\')"><i class="Hui-iconfont">&#xe6df;</i> 编辑</a>&nbsp;'
        	  		+'<a class="btn btn-danger btn-xs" href="javascript:;" title="删除" onclick="list_del(\''+menu.id+'\')"><i class="Hui-iconfont">&#xe6e2;</i> 删除</a>&nbsp;'
        	  		+'<a class="btn btn-success btn-xs" href="javascript:;" title="添加下级机构" onclick="list_add(\''+menu.id+'\')"><i class="Hui-iconfont">&#xe616;</i> 添加下级机构</a>'
					+"</td>";
					
		  var tr=$("<tr></tr>").addClass("treegrid-"+menu.id);
		  if(0!=menu.parentId && '0'!=menu.parentId){
			  tr.addClass("treegrid-parent-"+menu.parentId);
		  }
		  tr.appendTo($('#table_list tbody')).html(tdStr);
	  if(menu.isParent){
		  showTableBody(menu.children);
	  }
	}
}
function load_table(){
	$('#table_list tbody').text("");
	$.getJSON("${BASE}/admin/sys/SysDept/deptSelectTree",{}, function(str){
		if(str){		
			showTableBody(str);
			$('#table_list').treegrid({
				"treeColumn" : 1,
				"initialState" : "collapsed",
				"saveState" : true
			});
		} else {
			$("#table_list tbody").html("<tr><td colspan='7'>没有数据！</td></tr>");
		}
	});
}
$(function(){
	load_table();
})
</script>